<template>
	<view class="head">
		<image :src="info.image" mode="" class="head_image"></image>
	</view>

	<!-- <rui-icons :icon="item.category == 'path' || item.category == 'custom' ? 'right':'more'" color="#999"></rui-icons> -->
	<view class="control">
		<view class="control_item" @click="$rui.route('/pages/product/cup-c1/enter/enter')">
			<view class="control_item_name">
				添加指纹
			</view>
			<rui-icons icon="right" color="#999"></rui-icons>
		</view>
		<view class="control_item" @click="$rui.route('/pages/product/cup-c1/fingerprint/fingerprint')">
			<view class="control_item_name">
				指纹列表
			</view>
			<rui-icons icon="right" color="#999"></rui-icons>
		</view>
		<view class="control_item" @click="openCup">
			<view class="control_item_name">
				蓝牙解锁
			</view>
			<rui-icons icon="more" color="#999"></rui-icons>
		</view>

		<view class="control_item" @click="unbind">
			<view class="control_item_name">
				解绑设备
			</view>
			<rui-icons icon="more" color="#999"></rui-icons>
		</view>
		<view class="control_item" @click="restore">
			<view class="control_item_name">
				恢复出厂设置
			</view>
			<rui-icons icon="more" color="#999"></rui-icons>
		</view>
		<view class="control_item" @click="toOta">
			<view class="control_item_name">
				固件升级
			</view>
			<rui-icons icon="right" color="#999"></rui-icons>
			<view class="control_item_dot" v-if="product.getUpdate"></view>
		</view>
		<view class="control_item" @click="$rui.route('/pages/user/help/help')">
			<view class="control_item_name">
				常见问题
			</view>
			<rui-icons icon="right" color="#999"></rui-icons>
		</view>
	</view>

</template>

<script setup>
	import {
		ref
	} from "vue"
	import {
		onLoad,
		onShow,
		onHide,
		onUnload
	} from "@dcloudio/uni-app"

	import {
		getDeviceMenu,
		delFinger,
		delProduct,
		// addFinger,
		// getFingerAll,
		restoreFactory
	} from "@/common/api.js"
	import {
		createBuffer
	} from "@/util/common.js"

	import ble from "@/util/ble"

	import {
		productStore
	} from "@/store/index.js"

	let product = productStore();


	let info = ref({})
	let menu = ref([]);
	let command_timeout = ""

	onUnload(() => {
	
	})

	onLoad(opts => {
		info.value = product.getInfo;
		
	})

	const toOta = () => {
		uni.redirectTo({
			url: "/pages/product/cup-c1/ota/ota",
		});
		// uni.redirectTo({
		// 	url: "/pages/product/cup-c1/index/index"
		// })
	}

	// /**
	//  * 同步指纹
	//  */
	// const syncFinger = () => {
	// 	let number = product.getFingerprint;
	// 	getFingerAll({
	// 		user_product_id: info.value.id
	// 	}).then(res => {
	// 		/**
	// 		 * 判断数据库指纹数量与设备数量是否相符
	// 		 * 1、设备指纹数量未同步至数据库
	// 		 * 2、查找缺失的key
	// 		 * 3、提交设备录入的key
	// 		 */
	// 		let keys = res.data.keys;
	// 		if (res.data.number >= 10) return;
	// 		let k = [];
	// 		for (let key = 0; key <= number; key++) {
	// 			if (!keys.includes(key) && key > 0) {
	// 				//同步设备录入指纹
	// 				console.log('未同步指纹', key)
	// 				k.push(key)
	// 			}
	// 		}
	// 		if (!k.length) return false;
	// 		addFinger({
	// 			name: "设备指纹",
	// 			user_product_id: info.value.id,
	// 			keys: k.join(',')
	// 		})

	// 	})
	// }
	const restore = () => {
		uni.showModal({
			title: '警告提示',
			content: '您是否要清除设备上所有数据？',
			success: function(res) {
				if (res.confirm) {
					ble.send(createBuffer(21), () => {
						uni.showToast({
							title: "初始成功",
							icon: "success"
						})
						//删除指纹, 删除喝水次数
						restoreFactory({
							user_product_id: info.value.id
						})
						setTimeout(() => {
							uni.switchTab({
								url: "/pages/tabbar/index/index"
							})
						}, 1500)
					})


				}
			}
		});
	}
	const unbind = () => {
		uni.showModal({
			title: '警告提示',
			content: '您是否将此设备从小程序移除？',
			success: function(res) {
				if (res.confirm) {
					ble.send(createBuffer(21))
					//删除指纹, 删除喝水次数
					restoreFactory({
						user_product_id: info.value.id
					}).then(() => {
						delProduct({
							id: info.value.id
						}).then(res => {
							uni.showToast({
								title: "移除成功",
								icon: "success"
							})
							setTimeout(() => {
								ble.close()
								uni.switchTab({
									url: "/pages/tabbar/index/index"
								})
							}, 1500)
						})
					})

				}
			}
		});
	}

	const openCup = () => {
		uni.showLoading({
			title: '开锁中',
			mask: true,
		});

		ble.send(createBuffer(10), () => {
			setTimeout(() => {
				uni.hideLoading();
			}, 1000)
		})
	}


	const change = (e) => {
		//未操作超时 返回首页
		uni.$rui.timeout.clear()

		let msg = {
			10: "蓝牙开锁中",
			11: "指纹添加中",
			21: "初始化中",
		}

		if (e.category == 'custom') {
			uni.$rui.route(e.content, {
				user_product_id: info.value.id
			})
		}

		if (e.category == 'path') {
			uni.$rui.route(e.content, info.value)
		}

		if (e.category == 'instruct') {
			console.log('蓝牙指令', e)
			let command = instruct[e.instruct];

			if (e.content) {
				uni.showModal({
					title: '警告提示',
					content: e.content,
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '执行中',
								mask: true,
							});
							_run(() => {
								if (e.instruct == 'init') {
									delFinger({
										id: info.value.id,
										type: 'all'
									})
								}
								if (e.instruct == 'delete') {
									delProduct({
										id: info.value.id
									}).then(res => {
										uni.navigateBack({
											delta: 2
										})
									})
								}
							})
						}
					}
				});
				return false;
			} else {
				clearTimeout(command_timeout)
				uni.showLoading({
					title: msg[instruct[e.instruct]],
					mask: true,
					success() {
						command_timeout = setTimeout(() => {
							uni.hideLoading();
						}, 4 * 1000)
					}
				});
				_run()
			}

			function _run(callback) {
				console.log('执行命令：' + command)
				bluetooth.write(command, 0).then(res => {
					setTimeout(() => {
						uni.hideLoading();
					}, 1600)
					if (typeof callback == 'function') {
						callback()
					}
				}).catch(err => {
					uni.hideLoading();
					uni.showToast({
						title: "执行失败",
						mask: true
					})
				})
			}

		}

	}


	const getMenu = () => {
		uni.setNavigationBarTitle({
			title: '设置' + info.value.name || "设置设备"
		});
		getDeviceMenu({
			product_id: info.value.product_id
		}).then(res => {
			console.log(res)
			menu.value = res.data;
		})
	}

	// onUnload(() => {
	// 	bluetooth.end(info.value.id);
	// })
</script>

<style lang="scss" scoped>
	.control {
		margin-top: 50rpx;

		&_item {
			margin: 20rpx;
			background: #fff;
			padding: 40rpx;
			border-radius: 20rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: relative;

			&_dot {
				width: 20rpx;
				height: 20rpx;
				background: #f00;
				border-radius: 50%;
				position: absolute;
				top: 50rpx;
				right: 100rpx;
			}

			&:active {
				background: #eee;
			}
		}
	}

	.head {
		position: relative;
		padding: 30rpx;
		text-align: center;

		&_image {
			width: 300rpx;
			height: 300rpx;
		}
	}
</style>